# coding=utf-8

# 导入driver对象
from appium import webdriver
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction
import time
import random
import math


desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platfromVersion'] = '6.0'
# 通过adb devices获取
desired_caps['deviceName'] = '0123456789ABCDEF'
desired_caps['appPackage'] = 'com.southgnss.egstar3'
desired_caps['appActivity'] = 'com.southgnss.egstar.EGStarMainActivity'
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
desired_caps['noReset'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

time.sleep(3)
driver.unlock()
# 获取屏幕的size
size = driver.get_window_size()
# 获取屏幕宽度 width
width = size['width']
# 获取屏幕高度 height
height = size['height']
# 获取屏幕尺寸
screenX = driver.get_window_size()['width']
screenY = driver.get_window_size()['height']

# 缩小操作方法
def screen_pinch():
    firstFinger = TouchAction(driver)
    secondFinger = TouchAction(driver)
    zoom_finger = MultiAction(driver)
    firstFinger.press(x=width * 0.5, y=height * 0.4).wait(800).move_to(x=width * 0.5, y=width * 0.5).wait(800).release()
    secondFinger.press(x=width * 0.5, y=height * 0.6).wait(800).move_to(x=height * 0.5, y=height * 0.5).wait(
        800).release()
    zoom_finger.add(firstFinger, secondFinger)
    zoom_finger.perform()

# 放大操作方法
def screen_zoom():
    firstFinger = TouchAction(driver)
    secondFinger = TouchAction(driver)
    zoom_finger = MultiAction(driver)
    firstFinger.press(x=width * 0.5, y=height * 0.5).wait(800).move_to(x=width * 0.5, y=height * 0.4).wait(
        800).release()
    secondFinger.press(x=width * 0.5, y=height * 0.5).wait(800).move_to(x=width * 0.5, y=height * 0.6).wait(
        800).release()
    zoom_finger.add(firstFinger, secondFinger)
    zoom_finger.perform()

# 执行滑屏操作,向下（下拉）滑动
x1 = width * 0.5
y1 = height * 0.25
y2 = height * 0.8
time.sleep(1)
# print("滑动前")
driver.swipe(x1, y1, x1, y2)
# print("滑动后")
# 增加滑动次数，滑动效果不明显，增加滑动次数
# for i in range(1):
#     # print("第%d次滑屏"%i)
#     time.sleep(3)
#     driver.swipe(x1, y1, x1, y2)
# time.sleep(1)


# 封装滑动方法
def swipeUp(driver, n):
    '''定义向上滑动方法'''
    # print("定义向上滑动方法")
    x1 = width * 0.5
    y1 = height * 0.8
    y2 = height * 0.25
    time.sleep(1)
    # print("滑动前")
    for i in range(n):
        # print("第%d次滑屏" % i)
        time.sleep(1)
        driver.swipe(x1, y1, x1, y2)

def swipeDown(driver, n=1):
    '''定义向下滑动方法'''
    # print("定义向下滑动方法")
    x1 = width * 0.5
    y1 = height * 0.5
    y2 = height * 0.8
    time.sleep(3)
    # print("滑动前")
    for i in range(n):
        # print("第%d次滑屏" % i)
        time.sleep(1)
        driver.swipe(x1, y1, x1, y2)

def swipeLeft(driver, n=1):
    '''定义向左滑动方法'''
    # print("定义向左滑动方法")
    x1 = width * 0.8
    x2 = width * 0.2
    y1 = height * 0.15
    time.sleep(1)
    # print("滑动前")
    for i in range(n):
        # print("第%d次滑屏" % i)
        time.sleep(1)
        driver.swipe(x1, y1, x2, y1)

def swipeRight(driver, n=2):
    '''定义向右滑动方法'''
    # print("定义？向右滑动方法")
    x1 = width * 0.2
    x2 = width * 0.8
    y1 = height * 0.15
    time.sleep(1)
    # print("滑动前")
    for i in range(n):
        # print("第%d次滑屏" % i)
        time.sleep(3)
        driver.swipe(x1, y1, x2, y1)